package cn.stylefeng.guns.core.util;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 
 * @author zhucx
 *
 */
public class DateUtil2 {
    
	/**
	 * 得到工时
	 * @param endDate
	 * @param nowDate
	 * @return
	 * @throws ParseException
	 */
    public static String getDatePoorMin(String endDate, String nowDate) throws ParseException {

    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date d1 = df.parse(endDate);//结束时间
        Date d2 = df.parse(nowDate);//开始时间
        
    	// 获得两个时间的毫秒时间差异
    	BigDecimal diff = new BigDecimal(d1.getTime() - d2.getTime());
    	
    	BigDecimal a = new BigDecimal(1000);
    	BigDecimal b = new BigDecimal(60);
    	
    	BigDecimal miao = diff.divide(a);
    	
    	BigDecimal fenzhong = miao.divide(b,2,BigDecimal.ROUND_HALF_DOWN);
    	
    	BigDecimal hour = fenzhong.divide(b,2,BigDecimal.ROUND_HALF_DOWN);

    	return  hour+"";

    	}
	/**
	 * 得到分钟
	 * @param endDate
	 * @param nowDate
	 * @return
	 * @throws ParseException
	 */
    public static String getDatePoorMinfenzhong(String endDate, String nowDate) throws ParseException {

    	DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date d1 = df.parse(endDate);//结束时间
        Date d2 = df.parse(nowDate);//开始时间
        
    	// 获得两个时间的毫秒时间差异
    	BigDecimal diff = new BigDecimal(d1.getTime() - d2.getTime());
    	
    	BigDecimal a = new BigDecimal(1000);
    	BigDecimal b = new BigDecimal(60);
    	
    	BigDecimal miao = diff.divide(a);
    	
    	BigDecimal fenzhong = miao.divide(b,2,BigDecimal.ROUND_HALF_DOWN);
    	

    	return  fenzhong+"";

    	}
	
		/** 
	    * 获取过去第几天的日期 yyyy-MM-dd
	    * @param past 
	    * @return 
	    */  
	   public static String getPastDate(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       String result = format.format(today);  
	       return result;  
	   }  
		
		/** 
	    * 获取几天后的日期 yyyy-MM-dd
	    * @param past 
	    * @return 
	    */  
	   public static String getPastDatej(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       String result = format.format(today);  
	       return result;  
	   } 
		
		/** 
	    * 获取当前日期 yyyy-MM-dd
	    * @param past 
	    * @return 
	    */  
	   public static String getDate() {  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       return format.format(new Date());  
	   }   
		
		/** 
	    * 获取当前日期yyyy-MM-dd HH:mm:ss
	    * @param past 
	    * @return 
	    */  
	   public static String getDateS() {  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	       return format.format(new Date());  
	   }  
	   
	   public static String parseTimeZone(String dateString) {
	        String FORMAT_STRING = "yyyy-MM-dd";
	        String FORMAT_STRING2 = "EEE MMM dd HH:mm:ss z yyyy";
	        String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};
	        String SPLIT_STRING = "(中国标准时间)";
	        try {
	            dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
	            //转换为date
	            SimpleDateFormat sf1 = new SimpleDateFormat(FORMAT_STRING2, Locale.ENGLISH);
	            Date date = sf1.parse(dateString);
	            return new SimpleDateFormat(FORMAT_STRING).format(date);
	        } catch (Exception e) {
	            throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
	        }
	    }
	   
	   /**
	    * 得到上月月份
	    * yyyy-MM
	    * @return
	    */
	   public static String getLastMonth() {
	        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
	        Date date = new Date();
	        Calendar calendar = Calendar.getInstance();
	        calendar.setTime(date); // 设置为当前时间
	        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
	        date = calendar.getTime();
	        String accDate = format.format(date);
	        return accDate;
	    }
	   
}
